﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace SimpComm
{
    #region 例子 有点难用,待优化

    #region 卡类型
    /// <summary>
    /// 卡类型
    /// </summary>
    public class CardTypes1 : MyEnum<byte>
    {
        public static readonly CardTypes1 PubCard = new CardTypes1(0, "普通摊位卡");
        public static readonly CardTypes1 VipCard = new CardTypes1(1, "VIP摊位卡");
        public static readonly CardTypes1 PriCard = new CardTypes1(2, "才富充值卡");

        /// <summary>
        /// 自动生成List
        /// </summary>
        private static List<CardTypes1> _Items;
        public static List<CardTypes1> Items
        {
            get
            {
                if (_Items == null)
                {
                    var _Type = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
                    _Items =
                        _Type.GetFields().Where(field => field.FieldType == _Type).Select(
                            field => (CardTypes1)field.GetValue(null)).ToList();
                }
                return _Items;
            }
        }

        public static CardTypes1 Get(byte value)
        {
            return Items.SingleOrDefault(p => p.Value == value);
        }

        public static CardTypes1 Get(string descript)
        {
            return Items.SingleOrDefault(p => p.Text == descript);
        }

        //私有构造函数,有效阻止在类型外部生成实例
        private CardTypes1(byte value, string text)
        {
            _Value = value;
            _Text = text;
        }
    }
    #endregion

    #endregion

    #region 枚举类
    /// <summary>
    /// 枚举类基础类
    /// </summary>
    public abstract class MyEnum<T> 
        where T : struct
    {
        protected T _Value;
        public T Value { get { return _Value; } }

        protected string _Text;
        public string Text { get { return _Text; } }

        public override string ToString()
        {
            return Text;
        }
        //重载==符号
        public static bool operator ==(
            MyEnum<T> leftHandSide,
            MyEnum<T> rightHandSide)
        {
            if (ReferenceEquals(leftHandSide, null))
            {
                //Return true if rightHandSide is alse null
                //But false otherwise
                return ReferenceEquals(rightHandSide, null);
            }
            return (leftHandSide.Equals(rightHandSide));
        }
        //重载!=符号
        public static bool operator !=(
             MyEnum<T> leftHandSide,
             MyEnum<T> rightHandSide)
        {
            return !(leftHandSide == rightHandSide);
        }
        //重载Equals
        public override bool Equals(object obj)
        {
            var book = obj as MyEnum<T>;
            if (book == null)
            {
                return false;
            }
            return (this.Value.Equals(book.Value));
        }
        public override int GetHashCode()
        {
            return this.Value.GetHashCode() ^ this.Text.GetHashCode();
        }

    }

    #endregion
}
